package com.lg.atp.utils;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

import org.apache.commons.lang3.StringUtils;

import com.lg.atp.xml.XmlAnno;
import com.lg.atp.xml.XmlBean;


public class XmlUtils {

	
	public  static List<XmlBean> parseXmlToBean(String xml) throws Exception {
		List<XmlBean> list = new ArrayList<XmlBean>();
		 Map<String,Method> map =  parserClass();
		 InputStream is = new ByteArrayInputStream(xml.getBytes());     
	        XMLInputFactory xmlFactory  = XMLInputFactory.newInstance();  
	        XMLEventReader reader = xmlFactory.createXMLEventReader(is);  
	        while(reader.hasNext()){  
	            XMLEvent event = reader.nextEvent() ;  
	            if(event.isStartElement()){ //濡傛灉鏄紑濮嬭妭鐐�  
	                StartElement startEle = event.asStartElement() ;  
	                String eleName = startEle.getName().toString() ;     
	                    Iterator it  = startEle.getAttributes() ;  
	                    XmlBean bean = new XmlBean();
	                    while(it.hasNext()){  
	                        Attribute attr = (Attribute) it.next() ;  
	                        String attrName = attr.getName().toString();
	                        String value = attr.getValue();
	                        if(attrName != null && attrName.equals("name") && value == null)
	                        	continue;
	                        Method method = map.get(attrName);
		                    if(method == null)
		                    	continue;
	                        method.invoke(bean, value);
	                        if(list.contains(bean)) {
	                        		continue;
	                        }
	                        list.add(bean);
	                    }  
	            }  
	  
	        }
	        is.close();
	        map = null;
	        xmlFactory = null;
	        reader.close();
	        return list;
	}
	
	
	private static Map<String,Method> parserClass() throws IntrospectionException{
		Map<String,Method> map = new HashMap<String,Method>();
		Class cls = XmlBean.class;
		Field[] fields = cls.getDeclaredFields();
		for(Field field:fields){
			XmlAnno an=field.getAnnotation(XmlAnno.class);
			if(an == null)
				continue;
			String value = an.elementName();
			if(StringUtils.isNotBlank(value)){
				PropertyDescriptor pd = new PropertyDescriptor(field.getName(),cls); 
				Method setMethod = pd.getWriteMethod();
				map.put(value, setMethod);
			}
		}
		return map;
	}
	
}
